package com.drama.api.company.video.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.sdk.service.mts20140618.AsyncClient;
import com.aliyun.sdk.service.mts20140618.models.*;
import com.drama.api.base.Constants;
import com.drama.api.base.RedisUtils;
import com.drama.api.base.Type;
import com.drama.api.company.company.dao.CompanyDao;
import com.drama.api.company.company.domain.po.CompanyPO;
import com.drama.api.company.dramas.dao.VideoLogDao;
import com.drama.api.company.video.dao.CompanyVideoDao;
import com.drama.api.company.video.domain.po.CompanyVideoPO;
import com.drama.api.company.video.domain.vo.*;
import com.drama.api.company.video.service.ICompanyVideoService;
import com.drama.api.sys.video.dao.ResourceFileDao;
import com.drama.api.sys.video.domain.po.ResourceFilePO;
import com.drama.api.task.DeleteFileThread;
import com.drama.exception.BusinessException;
import com.drama.util.KeyUtil;
import com.drama.util.RestTemplateUtils;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;


@Service
@Transactional
@Slf4j
public class CompanyVideoServiceImpl implements ICompanyVideoService {

    @Autowired
    private CompanyVideoDao companyVideoDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private ResourceFileDao resourceFileDao;
    @Value("${resource.api.url}")
    private String resource_server_url;

    @Override
    public List<CompanyVideoVO> getVideosList(CompanyVideoSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        List<CompanyVideoVO> list=companyVideoDao.getVideosList(searchVO);
        if(list!=null&&list.size()>0){
            for (CompanyVideoVO companyVideoVO:list){
                companyVideoVO.setVideoOldUrl(KeyUtil.encryptFromString(companyVideoVO.getVideoOldUrl()));
            }
        }
        return list;
    }


    @Override
    public List<CompanyVideoVO> getRecycleList(CompanyVideoSearchVO searchVO) {
        List<CompanyVideoVO> list=companyVideoDao.getRecycleList(searchVO);
        if(list!=null&&list.size()>0){
            for (CompanyVideoVO companyVideoVO:list){
                companyVideoVO.setVideoOldUrl(KeyUtil.encryptFromString(companyVideoVO.getVideoOldUrl()));
            }
        }
        return list;
    }

    @Override
    public void recover(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        companyVideoPO.setDelFlag(Boolean.FALSE);
        companyVideoPO.preUpdate();
        int i = companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void deletes(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO.getVideoState()==1){
            throw new BusinessException(Type.FAIL.getCode(),  "已经选取的视频目前不能删除！");
        }
//        companyVideoPO.preDelete();
//        int i = companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
//        if (i == Constants.SQL_RETURN_SUCCESS) {
//            throw new BusinessException(Type.FAIL);
//        }
        companyVideoDao.deleteByPrimaryKey(id);
        //退还存储空间
        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyVideoPO.getCompanyId());
        if(StringUtils.isBlank(companyPO.getStorageNum())){
            companyPO.setStorageNum(BigDecimal.ZERO.toString());
        }
        ResourceFilePO resourceFilePO=resourceFileDao.selectByPrimaryKey(companyVideoPO.getVideoFileId());
        companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(resourceFilePO.getFileSize())).toString());
        Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,resourceFilePO.getFileFullPath()));
        resourceFileDao.deleteByPrimaryKey(resourceFilePO.getId());
        //如果有转码视频
        if(StringUtils.isNotBlank(companyVideoPO.getVideoSize())&&StringUtils.isNotBlank(companyVideoPO.getVideoUrl())){
            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyVideoPO.getVideoSize())).toString());
            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyVideoPO.getVideoUrl()));
        }
        companyPO.preUpdate();
        resourceFileDao.deleteByPrimaryKey(companyVideoPO.getVideoFileId());
        companyDao.updateByPrimaryKeySelective(companyPO);
    }

    @Override
    public void start_examine(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),  "视频不存在！");
        }
        if(companyVideoPO.getIsExamine()!=2){
            throw new BusinessException(Type.FAIL.getCode(),  "视频已经发起审核，请勿重复操作！");
        }
        if(companyVideoPO.getIsOss()!=1){
            this.aliVideo(companyVideoPO.getVideoOldUrl());
            companyVideoPO.setIsOss(1);
        }
        String jopId=this.vide_examine(companyVideoPO.getVideoOldUrl());
        redisUtils.set("examine_jop"+id,jopId);
        companyVideoPO.setIsExamine(3);
        companyVideoPO.preUpdate();
        companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);

    }

    @Autowired
    RedisUtils redisUtils;

    @Override
    public void start_label(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),  "视频不存在！");
        }
        if(companyVideoPO.getIsLabel()!=2){
            throw new BusinessException(Type.FAIL.getCode(),  "视频已经发起智能标签，请勿重复操作！");
        }
        if(companyVideoPO.getIsOss()!=1){
            this.aliVideo(companyVideoPO.getVideoOldUrl());
            companyVideoPO.setIsOss(1);
        }
        String jopId=this.vide_label(companyVideoPO.getVideoOldUrl());
        redisUtils.set("label_jop"+id,jopId);
        companyVideoPO.setIsLabel(3);
        companyVideoPO.preUpdate();
        companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
    }

    @Override
    public void start_cropping(VideoCropping videoCropping) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(videoCropping.getId());
        if(companyVideoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),  "视频不存在！");
        }
        if(companyVideoPO.getIsCropping()==3){
            throw new BusinessException(Type.FAIL.getCode(),  "视频已经发起剪辑中，请勿重复操作！");
        }
        if(companyVideoPO.getIsOss()!=1){
            this.aliVideo(companyVideoPO.getVideoOldUrl());
            companyVideoPO.setIsOss(1);
        }
        String jopId=  this.video_cropping(companyVideoPO.getVideoOldUrl(),
                new BigDecimal(videoCropping.getStartTime()).divide(new BigDecimal("1000")).setScale(3).toString()
               , new BigDecimal(videoCropping.getEndTime()).divide(new BigDecimal("1000")).setScale(3).toString());
        redisUtils.set("cropping_jop"+companyVideoPO.getId(),jopId);
        companyVideoPO.setIsCropping(3);
        companyVideoPO.preUpdate();
        companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
    }

    @Override
    public void synchronous(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO==null){
            throw new BusinessException(Type.FAIL.getCode(),  "视频不存在！");
        }
        if(companyVideoPO.getIsSynchronous()!=2){
            throw new BusinessException(Type.FAIL.getCode(),  "视频已经下发同步，请勿重复操作！");
        }
        companyVideoPO.setIsSynchronous(3);
        companyVideoPO.preUpdate();
        companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
    }

    @Override
    public void checkName(String name) {
        checkCompany();
//        CompanyPO companyPO = companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("videoName",name).andEqualTo("companyId",StpUtil.getLoginId());
        example.and().andEqualTo("videoAllName",name).andEqualTo("companyId",StpUtil.getLoginId());
        if(companyVideoDao.selectCountByExample(example)>0){
            throw new BusinessException(Type.FAIL.getCode(),  "视频名称有重复！");
        }
    }

    //AccessKey ID
    //LTAI5t7HSfSSA7R2ypyedBzB
    //AccessKey Secret
    //nZQFiclFvvSH9duU6py9kyFvRlfccB



    public static void main(String[] args) {
//        aliVideo("http://video.yujuyi.com/history/1731562976198000640/20231204_刘锐.mp4");
       video_cropping("http://video.yujuyi.com/history/1731562976198000640/20231204_刘锐.mp4","1.002","22.001");
    }
    String aliUrl="https://yujuyi.oss-cn-beijing.aliyuncs.com/";


    public static String video_cropping(String videoUrl, String seek, String duration){
        String objectName =videoUrl.replaceAll("http://video.yujuyi.com/","");
        try {
            Credential credential=Credential.builder()
                    .accessKeyId("LTAI5t7HSfSSA7R2ypyedBzB")
                    .accessKeySecret("nZQFiclFvvSH9duU6py9kyFvRlfccB")
                    .build();
            StaticCredentialProvider provider = StaticCredentialProvider.create(credential);

            AsyncClient client = AsyncClient.builder()
                    .region("cn-beijing") // Region ID
                    .credentialsProvider(provider)
                    .overrideConfiguration( ClientOverrideConfiguration.create() .setEndpointOverride("mts.cn-beijing.aliyuncs.com"))
                    .build();
            // 创建PutObjectRequest对象。
            JSONObject inputObject=new JSONObject();
            inputObject.put("Bucket","yujuyi");
            inputObject.put("Location","oss-cn-beijing");
            inputObject.put("Object",objectName);

            JSONObject clip = new JSONObject();
            JSONObject timeSpan=new JSONObject();
            timeSpan.put("Seek",seek);
            timeSpan.put("Duration",duration);
            clip.put("TimeSpan", timeSpan);
            clip.put("ConfigToClipFirstPart", true);

            JSONArray jsonArray=new JSONArray();
            JSONObject outObject=new JSONObject();
            outObject.put("OutputObject",objectName.substring(0,objectName.lastIndexOf("."))+"_oox2xoo"+objectName.substring(objectName.lastIndexOf(".")));
            outObject.put("Format","oss-cn-beijing");
            outObject.put("Object",objectName);
            outObject.put("Clip",clip);
            jsonArray.add(outObject);

            SubmitJobsRequest submitJobsRequest = SubmitJobsRequest.builder()
                    .pipelineId("25b04207d91a43feaeb86653414703cc")
                    .outputBucket("yujuyi")
                    .input(inputObject.toJSONString())
                    .outputs(jsonArray.toJSONString())
                    .outputLocation("oss-cn-beijing")
                    .build();
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // 上传字符串。
            CompletableFuture<SubmitJobsResponse> response = client.submitJobs(submitJobsRequest);
            // Synchronously get the return value of the API request
            SubmitJobsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
            return resp.getBody().getJobResultList().getJobResult().get(0).getJob().getJobId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String vide_label(String videoUrl) {
        String objectName = videoUrl.replaceAll("http://video.yujuyi.com/", "");
        try {
            Credential credential = Credential.builder()
                    .accessKeyId("LTAI5t7HSfSSA7R2ypyedBzB")
                    .accessKeySecret("nZQFiclFvvSH9duU6py9kyFvRlfccB")
                    .build();
            StaticCredentialProvider provider = StaticCredentialProvider.create(credential);

            AsyncClient client = AsyncClient.builder()
                    .region("cn-beijing") // Region ID
                    .credentialsProvider(provider)
                    .overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride("mts.cn-beijing.aliyuncs.com"))
                    .build();
            // 创建PutObjectRequest对象。
            SubmitSmarttagJobRequest submitMediaCensorJobRequest = SubmitSmarttagJobRequest.builder()
                    .input("https://yujuyi.oss-cn-beijing.aliyuncs.com/" + objectName)
                    .pipelineId("757ba211eba2447faa21e52161d97c3e")
                    .title(videoUrl.substring(videoUrl.lastIndexOf(".")))
                    .build();
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // 上传字符串。
            CompletableFuture<SubmitSmarttagJobResponse> response = client.submitSmarttagJob(submitMediaCensorJobRequest);
            // Synchronously get the return value of the API request
            SubmitSmarttagJobResponse resp = response.get();
            log.info(resp.getBody().getJobId());
            System.out.println(new Gson().toJson(resp));
            return resp.getBody().getJobId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String vide_examine(String videoUrl){
        String objectName =videoUrl.replaceAll("http://video.yujuyi.com/","");
        try {
            Credential credential=Credential.builder()
                    .accessKeyId("LTAI5t7HSfSSA7R2ypyedBzB")
                    .accessKeySecret("nZQFiclFvvSH9duU6py9kyFvRlfccB")
                    .build();
            StaticCredentialProvider provider = StaticCredentialProvider.create(credential);

            AsyncClient client = AsyncClient.builder()
                    .region("cn-beijing") // Region ID
                    .credentialsProvider(provider)
                    .overrideConfiguration( ClientOverrideConfiguration.create() .setEndpointOverride("mts.cn-beijing.aliyuncs.com"))
                    .build();
            // 创建PutObjectRequest对象。

            JSONObject configObject=new JSONObject();
            JSONArray jsonArray=new JSONArray();
            jsonArray.add("porn");
            jsonArray.add("terrorism");
            jsonArray.add("ad");
            jsonArray.add("live");
            configObject.put("Scenes",jsonArray);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("Bucket","yujuyi");
            jsonObject.put("Location","oss-cn-beijing");
            jsonObject.put("Object",objectName.substring(0,objectName.lastIndexOf("."))+"/example-{Count}.jpg");
            configObject.put("OutputFile",jsonObject);
            configObject.put("SaveType","abnormal");
            configObject.put("BizType","common");
            log.info(configObject.toJSONString());
            SubmitMediaCensorJobRequest submitMediaCensorJobRequest = SubmitMediaCensorJobRequest.builder()
                    .input("https://yujuyi.oss-cn-beijing.aliyuncs.com/"+objectName)
                    .pipelineId("3e3a9d00f4964eacb4990e47f769065b")
                    .videoCensorConfig(configObject.toJSONString())
                    .build();
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // 上传字符串。
            CompletableFuture<SubmitMediaCensorJobResponse> response = client.submitMediaCensorJob(submitMediaCensorJobRequest);
            // Synchronously get the return value of the API request
            SubmitMediaCensorJobResponse resp = response.get();
            log.info(resp.getBody().getJobId());
            System.out.println(new Gson().toJson(resp));

            return resp.getBody().getJobId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void aliVideo(String videoUrl){
        OSS ossClient = null;
        try {
//            String videoUrl="http://video.yujuyi.com/history/1730037292707364864/20231130_1727692043402137600.mp4";
            InputStream inputStream = new URL(videoUrl).openStream();
            String endpoint = "https://oss-cn-beijing.aliyuncs.com";
            String bucketName = "yujuyi";
            String objectName =videoUrl.replaceAll("http://video.yujuyi.com/","");
            //"history/1730037292707364864/20231130_1727692043402137600.mp4";
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, "LTAI5t7HSfSSA7R2ypyedBzB","nZQFiclFvvSH9duU6py9kyFvRlfccB");
            // 填写字符串。
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);

            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // 上传字符串。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            log.info(result.toString());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }



    @Autowired
    private VideoLogDao videoLogDao;

    @Override
    public List<CompanyVideoVO> getVideosLogList(CompanyVideoSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        List<CompanyVideoVO> list=videoLogDao.getList(searchVO);
        if(list!=null&&list.size()>0){
            for (CompanyVideoVO companyVideoVO:list){
                companyVideoVO.setVideoOldUrl(KeyUtil.encryptFromString(companyVideoVO.getVideoOldUrl()));
            }
        }
        return list;
    }



    private void checkCompany() {
        CompanyPO companyPO = companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        if (companyPO == null) {
            throw new BusinessException(Type.PERMISSIONS_VALIDATE_FAIL);
        }
        if (companyPO.getIsUpload() == null || companyPO.getIsUpload() != 1) {
            throw new BusinessException(Type.FAIL.getCode(), "当前账号没有上传视频权限！");
        }
    }

    @Override
    public List<CompanyVideoVO> getList(CompanyVideoSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        List<CompanyVideoVO> list=companyVideoDao.getList(searchVO);
        if(list!=null&&list.size()>0){
            for (CompanyVideoVO companyVideoVO:list){
                companyVideoVO.setVideoOldUrl(KeyUtil.encryptFromString(companyVideoVO.getVideoOldUrl()));
            }
        }
        return list;
    }


    @Override
    public void add(CompanyVideoAddVO vo) {
        checkCompany();
        String maxRef=companyVideoDao.getMaxRef();
        if(StringUtils.isBlank(maxRef)){
            maxRef="V0001";
        }
        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("videoName",vo.getVideoName())
                .andEqualTo("companyId",StpUtil.getLoginId());
        if(companyVideoDao.selectCountByExample(example)>0){
            throw new BusinessException(Type.FAIL.getCode(),  "视频名称不能重复！");
        }
        maxRef="V"+NumberUtil.decimalFormat("0000", new BigDecimal(maxRef.replaceAll("V","0")).add(BigDecimal.ONE).intValue());
        CompanyVideoPO companyVideoPO=new CompanyVideoPO();
        companyVideoPO.setCompanyId((String) StpUtil.getLoginId());
        companyVideoPO.setVideoLike(0L);
        companyVideoPO.setVideoPlay(0L);
        companyVideoPO.setVideoState(2);
        companyVideoPO.setVideoRef(maxRef);
        companyVideoPO.setVideoPrice(BigDecimal.ZERO);
        BeanUtils.copyProperties(vo, companyVideoPO);

        String videoName = vo.getVideoName();
        if (vo.getVideoName().contains(".")) {
            videoName = vo.getVideoName().substring(0,vo.getVideoName().lastIndexOf("."));// 获取文件的后缀名
        }
        companyVideoPO.setVideoName(videoName);
        companyVideoPO.setVideoAllName(vo.getVideoName());
        companyVideoPO.preInsert();
        int i = companyVideoDao.insertSelective(companyVideoPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }

        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyVideoPO.getCompanyId());
        ResourceFilePO resourceFilePO=resourceFileDao.selectByPrimaryKey(companyVideoPO.getVideoFileId());
        companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).add(new BigDecimal(resourceFilePO.getFileSize())).toString());
        companyPO.preUpdate();
        companyDao.updateByPrimaryKeySelective(companyPO);
    }

    @Override
    public void update(CompanyVideoUpdateVO vo) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(vo.getId());
        Example example=new Example(CompanyVideoPO.class);
        example.and().andEqualTo("videoName",vo.getVideoName());
        List<CompanyVideoPO> oldCompanyVideoPOList=companyVideoDao.selectByExample(example);
        if(oldCompanyVideoPOList.size()>1){
            throw new BusinessException(Type.FAIL.getCode(),  "视频名称不能重复！");
        }
        if(oldCompanyVideoPOList.size()==1){
            if(oldCompanyVideoPOList.get(0)!=null&&!oldCompanyVideoPOList.get(0).getId().equals(vo.getId())){
                throw new BusinessException(Type.FAIL.getCode(),  "视频名称不能重复！");
            }
        }
        BeanUtils.copyProperties(vo, companyVideoPO);
        companyVideoPO.setRemarks(DateUtil.format(new Date(),"yyyy-mm-dd")+"改");
        companyVideoPO.setVideoAllName(vo.getVideoName()+companyVideoPO.getVideoAllName().substring(companyVideoPO.getVideoAllName().lastIndexOf(".")));
        int i = companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public List<CompanyDramaVideoVO> geVideoList(CompanyDramaVideoSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        if(searchVO.getVideoState()==null){
            searchVO.setVideoState(2);
        }
        List<CompanyDramaVideoVO> list=companyVideoDao.geVideoList(searchVO);
        return list;
    }

    @Override
    public void delete(String id) {
        CompanyVideoPO companyVideoPO=companyVideoDao.selectByPrimaryKey(id);
        if(companyVideoPO.getVideoState()==1){
            throw new BusinessException(Type.FAIL.getCode(),  "已经选取的视频目前不能删除！");
        }
        companyVideoPO.preDelete();
        int i = companyVideoDao.updateByPrimaryKeySelective(companyVideoPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }

//        companyVideoDao.deleteByPrimaryKey(id);
//        //退还存储空间
//        CompanyPO companyPO=companyDao.selectByPrimaryKey(companyVideoPO.getCompanyId());
//        if(StringUtils.isBlank(companyPO.getStorageNum())){
//            companyPO.setStorageNum(BigDecimal.ZERO.toString());
//        }
//        ResourceFilePO resourceFilePO=resourceFileDao.selectByPrimaryKey(companyVideoPO.getVideoFileId());
//        companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(resourceFilePO.getFileSize())).toString());
//        Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,resourceFilePO.getFileFullPath()));
//        resourceFileDao.deleteByPrimaryKey(resourceFilePO.getId());
//        //如果有转码视频
//        if(StringUtils.isNotBlank(companyVideoPO.getVideoSize())&&StringUtils.isNotBlank(companyVideoPO.getVideoUrl())){
//            companyPO.setStorageNum(new BigDecimal(companyPO.getStorageNum()).subtract(new BigDecimal(companyVideoPO.getVideoSize())).toString());
//            Constants.executor.execute(new DeleteFileThread(new RestTemplateUtils(),resource_server_url,companyVideoPO.getVideoUrl()));
//        }
//        companyPO.preUpdate();
//        resourceFileDao.deleteByPrimaryKey(companyVideoPO.getVideoFileId());
//        companyDao.updateByPrimaryKeySelective(companyPO);

    }

}